﻿// Decompiled with JetBrains decompiler
// Type: Netick.ReflectionData
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

#nullable disable
namespace Netick;

public sealed class ReflectionData
{
  internal Dictionary<int, NetickUniqueObjectMeta> HashToObjectMetaData = new Dictionary<int, NetickUniqueObjectMeta>(64 /*0x40*/);
  internal List<Type> IdToInputType = new List<Type>(64 /*0x40*/);
  internal List<int> IdToInputSizeBytes = new List<int>(64 /*0x40*/);
  internal Dictionary<Type, int> InputTypeToId = new Dictionary<Type, int>(64 /*0x40*/);
  internal List<Type> NetworkBehaviourTypes = new List<Type>(64 /*0x40*/);
  internal List<Type> NetickBehaviourTypes = new List<Type>(64 /*0x40*/);
  internal Dictionary<Type, int> ScriptToOrder;
  public List<Assembly> Assemblies;
  internal Dictionary<MethodInfo, MethodInfo> RpcMethods;
  internal Type LagCompType;

  public ReflectionData()
  {
    this.RpcMethods = new Dictionary<MethodInfo, MethodInfo>();
    this.Assemblies = ((IEnumerable<Assembly>) AppDomain.CurrentDomain.GetAssemblies()).ToList<Assembly>();
    int num = 0;
    foreach (Assembly assembly in this.Assemblies)
    {
      if (((IEnumerable<AssemblyName>) assembly.GetReferencedAssemblies()).FirstOrDefault<AssemblyName>((Func<AssemblyName, bool>) (x => x.Name.Contains("Netick"))) != null)
      {
        foreach (Type type in assembly.GetTypes())
        {
          if (typeof (INetworkInput).IsAssignableFrom(type) && type != typeof (INetworkInput))
          {
            this.InputTypeToId.Add(type, num);
            this.IdToInputType.Add(type);
            this.IdToInputSizeBytes.Add(NetickUtils.GetWordSize(Marshal.SizeOf(type)) * 4);
            num += 10;
          }
          if (typeof (INetickScript).IsAssignableFrom(type))
            this.NetickBehaviourTypes.Add(type);
          if (typeof (INetickNetworkScript).IsAssignableFrom(type))
          {
            this.NetworkBehaviourTypes.Add(type);
            foreach (MethodInfo method1 in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
            {
              MethodInfo method2 = type.GetMethod(method1.Name + "__rpcInvoker", BindingFlags.Static | BindingFlags.NonPublic);
              if (method2 != (MethodInfo) null)
                this.RpcMethods.Add(method2, method1);
            }
          }
        }
      }
    }
    this.RpcMethods = this.RpcMethods.OrderBy<KeyValuePair<MethodInfo, MethodInfo>, string>((Func<KeyValuePair<MethodInfo, MethodInfo>, string>) (pair => NetickUtils.GetMethodName(pair.Value))).ToDictionary<KeyValuePair<MethodInfo, MethodInfo>, MethodInfo, MethodInfo>((Func<KeyValuePair<MethodInfo, MethodInfo>, MethodInfo>) (pair => pair.Key), (Func<KeyValuePair<MethodInfo, MethodInfo>, MethodInfo>) (pair => pair.Value));
    this.NetickBehaviourTypes = this.NetickBehaviourTypes.OrderBy<Type, string>((Func<Type, string>) (x => x.FullName)).ToList<Type>();
    this.ScriptToOrder = ExecutionOrderSystem.ProcessAndGetOrders(this.NetickBehaviourTypes);
    string lagCompClassName = "Netick.Pro.LagCompensation";
    Type type1 = ((IEnumerable<Assembly>) this.Assemblies.ToArray()).Reverse<Assembly>().Select<Assembly, Type>((Func<Assembly, Type>) (assembly => assembly.GetType(lagCompClassName))).FirstOrDefault<Type>((Func<Type, bool>) (t => t != (Type) null));
    if ((object) type1 == null)
      type1 = ((IEnumerable<Assembly>) this.Assemblies.ToArray()).Reverse<Assembly>().SelectMany<Assembly, Type>((Func<Assembly, IEnumerable<Type>>) (assembly => (IEnumerable<Type>) assembly.GetTypes())).FirstOrDefault<Type>((Func<Type, bool>) (t => t.Name == lagCompClassName));
    this.LagCompType = type1;
  }

  public bool AssemblyIsLoaded(string name)
  {
    foreach (Assembly assembly in this.Assemblies)
    {
      if (assembly.FullName.Contains(name))
        return true;
    }
    return false;
  }
}
